<?php


namespace app\common\model;


use app\common\controller\SampleController;
use app\lib\exception\BaseException;
use think\facade\Cache;
use tools\jwt\Token;

class User extends BaseModel
{
    //自动写入时间
    protected $autoWriteTimestamp = true;

    /**
     * /// 绑定第三方登录
     * @return \think\model\relation\HasMany
     */
    public function userbind(){
        return $this->hasMany('UserBind');
    }

    /**
     *  // 绑定用户信息表
     * @return \think\model\relation\HasOne
     */
    public function userinfo(){
        return $this->hasOne('UserInfo');
    }

    /**
     * 发送验证码
     * @return mixed
     * @throws BaseException
     */
    public function sendCode()
    {
        //获取用户提交的手机号
        $phone = request()->param('phone');
        //判断是否已经发过验证码
        if(Cache::get($phone)) throw new BaseException(['code'=>200,'msg'=>'不要频繁获取哦！','errorCode'=>30001]);
        //没有获取过
        //生成6位验证码
        $code = random_int(100000,999999);
        //判断是否开启验证码功能
        if(!config('api.aliSMS.isopen')){
            //没开启
            Cache::set($phone,$code,config('api.aliSMS.expire'));
            throw new BaseException(['code'=>200,'msg'=>'验证码:'.$code,'errorCode'=>30005]);
        }
        //发送验证码
        $res = SampleController::main($phone);
        //发送成功 写入缓存
        if($res['Code'] == 'OK') return Cache::set($phone,$code,config('api.aliSMS.expire'));
        //无效号码
        if($res['Code'] == 'isv.MOBILE_NUMBER_ILLEGAL') throw new BaseException(['code'=>200,'msg'=>'无效号码','errorCode'=>30002]);
        if($res['Code']=='isv.DAY_LIMIT_CONTROL') throw new BaseException(['code'=>200,'msg'=>'今日你已经发送超过限制，改日再来','errorCode'=>30003]);
        // 发送失败
        throw new BaseException(['code'=>200,'msg'=>'发送失败','errorCode'=>30004]);
    }

    /**
     * 手机号登录
     * @return array
     * @throws BaseException
     */

    public function phoneLogin()
    {
        //获取所有参数
        $param = request()->param();
        //验证用户是否存在
        $user = $this->isExists(['phone'=>$param['phone']]);
        //用户不存在直接注册
        if(!$user){
            //用户主表
            $user = self::create([
               'username' => $param['phone'],
               'phone' => $param['phone'],
                // 'password'=>password_hash($param['phone'],PASSWORD_DEFAULT)
            ]);
            //在用户信息表创建对应的记录(用于存储用户其他信息)
            $user->userinfo()->create(['user_id'=>$user->id]);
            $user->logintype = 'phone';
            return $this->CreateSaveToken($user->toArray());
        }
        //存在用户
        //判断用户是否被禁用
        $this->checkStatus($user->toArray());
        //登录成功返回token
        return $this->CreateSaveToken($user->toArray());
    }
    /**
     * 账号密码登录
     */
    public function login()
    {
        //获取所有参数
        $param = request()->param();
        //验证用户是否存在
        $user = $this->isExists($this->filterUserData($param['username']));
        //用户不存在
        if(!$user) throw new BaseException(['code'=>200,'msg'=>'用户名不存在','errorCode' => 20000]);
        //用户是否被禁用
        $this->checkStatus($user->toArray());
        //验证密码
        $this->checkPassword($param['password'],$user->password);
        //登陆成功 生成token ，进行缓存 ，返回客户端
        return $this->CreateSaveToken($user->toArray());
    }
}